{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "49ee6311-aa87-4b94-afff-c191eb030e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas import Series,DataFrame\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12d307ef-9933-4dd4-bdfb-2d58b0200531",
   "metadata": {},
   "source": [
    "### 1、通过一维数组创建Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0176ab0b-6904-4175-ac85-965e849557c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "05c4f42d-ac91-4ab5-9c5a-0e5a4680f46f",
   "metadata": {},
   "outputs": [],
   "source": [
    "series01 = Series(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4bd3b969-2426-4a2f-9ce5-9471c6eb93db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "dtype: int32"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series01 ## 将一维数组的索引值纳入series的第0列，其值为第1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a87b12c6-493c-405b-9d3b-683e592f41b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=4, step=1)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series01.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "93e5b0fc-ae44-4f16-8cba-457374809859",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series01.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5072986e-8c44-4508-a2e3-17f466ddec1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series01.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aab0ae9a-4f0c-4954-b30a-f15a0996227e",
   "metadata": {},
   "source": [
    "默认索引可以通过赋值方式修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "536e490c-d2f2-49fd-81db-02c661550ac4",
   "metadata": {},
   "outputs": [],
   "source": [
    "series02 = Series([34.5,56.78,45.67])\n",
    "series02.index = ['语文','数学','英语']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b0d72be4-4d61-4c89-ac6a-00a635acbd7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    34.50\n",
       "数学    56.78\n",
       "英语    45.67\n",
       "dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0ff4982-ae25-4df0-9022-c16135f48bfa",
   "metadata": {},
   "source": [
    "### 2、通过字典的方式创建Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "d52dea4a-26b2-42c6-b870-78ff2ace953e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_dict = {'20071001':6789.98,'20071002':3456.78,'20071003':3467.556}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "135eded8-a354-4eeb-aeb1-d6200681870a",
   "metadata": {},
   "outputs": [],
   "source": [
    "series04 = Series(a_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d6b6754e-1411-483f-9423-d23408571b18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20071001    6789.980\n",
       "20071002    3456.780\n",
       "20071003    3467.556\n",
       "dtype: float64"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series04"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa260b4d-3fd8-4443-abda-8410ffa3559e",
   "metadata": {},
   "source": [
    "### 3、Series 应用Numpy数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "fa061b70-2495-4801-994b-a8e727834748",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6789.98"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series04['20071001']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ca77d9b6-0aa6-444e-8d56-c401ace4cba9",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_13040\\3766849197.py:1: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  series04[0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6789.98"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series04[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "020cf758-7b1d-4814-bc67-6c72b7303caa",
   "metadata": {},
   "source": [
    "Numpy中的数组运算，在Series中都保留使用，并且Series进行数组运算时，\n",
    "索引与值之间的关系不会改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "cefc729e-5057-4a45-97d9-7199965bbe45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20071001    6789.980\n",
       "20071002    3456.780\n",
       "20071003    3467.556\n",
       "dtype: float64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "71a4614a-ac11-4574-868b-d552bfd2092e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20071002    3456.780\n",
       "20071003    3467.556\n",
       "dtype: float64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series04[series04<5000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "c4e2c1c9-663b-4a84-a5e7-24d8b5db0573",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20071001    67.89980\n",
       "20071002    34.56780\n",
       "20071003    34.67556\n",
       "dtype: float64"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series04 / 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "1a082ec3-6f07-4067-a8dc-24f995183cf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     2.718282\n",
       "1     7.389056\n",
       "2    20.085537\n",
       "3    54.598150\n",
       "dtype: float64"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(series01) ### 计算 series01 中每个元素的指数函数值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdc3c0ac-7a69-45e7-a0f0-0c28d1c97788",
   "metadata": {},
   "source": [
    "### 4、Series缺失值检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "5d680905-7252-4c2b-8b5b-ce6f4cf152d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = Series({'Tom':89,'Jhon':88,'Merry':96,'Max':65})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "e33477eb-f521-44a7-a443-aa64048067aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tom      89\n",
       "Jhon     88\n",
       "Merry    96\n",
       "Max      65\n",
       "dtype: int64"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "5917e558-804e-4466-902c-bdaf80fd89af",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_index = ['Tom','Max','Joe','Jhon','Merry']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "af8d7803-50f4-417e-b3ff-4d4805285733",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = Series(scores,index=new_index) ### 注意 是逗号,  而不是."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "4c145348-ff02-4dbc-a476-b42fb2fe695e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tom      89.0\n",
       "Max      65.0\n",
       "Joe       NaN\n",
       "Jhon     88.0\n",
       "Merry    96.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores # 键值对缺失值的，用NaN表示"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6086209d-2d1d-4635-bf75-538eadc376d7",
   "metadata": {},
   "source": [
    "pandas中的isnull和notnull函数都可以用来Series缺失值的检测\n",
    "isnull和notnull都返回一个布尔类型的Series\n",
    "不同的是\n",
    "ismull是过滤出缺失值的项，即只显示缺失的\n",
    "notnull是将缺失的项滤除，即只显示完整的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "46860a29-2da8-4d1f-9d80-b0bf92e2def8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Joe   NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[pd.isnull(scores)] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "edfc5ab7-ed67-42e3-85bc-2ff665e7b83d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tom      89.0\n",
       "Max      65.0\n",
       "Jhon     88.0\n",
       "Merry    96.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[pd.notnull(scores)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdcf5fd7-22dc-4ab5-baba-40a89227b8cc",
   "metadata": {},
   "source": [
    "### 5、Series 自动对齐\n",
    "不同Series之间进行算数运算，会自动对齐不同索引的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "0973f199-2b5e-440e-93a5-9ffef5603388",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_num = Series([23,45,67,89],\n",
    "                     index=['p3','p1','p2','p5'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "1bc9571a-5996-43de-bf83-de107ec79744",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_price_table = Series([9.98,2.34,4.56,5.67,8.78],\n",
    "                             index=['p1','p2','p3','p4','p5'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "0f041794-0029-41c1-a48e-dcc8a213cc97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "p1    449.10\n",
       "p2    156.78\n",
       "p3    104.88\n",
       "p4       NaN\n",
       "p5    781.42\n",
       "dtype: float64"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_sum = product_num * product_price_table\n",
    "product_sum"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "419c6a6d-d029-41b2-9452-0d00c2759291",
   "metadata": {},
   "source": [
    "### 6、Series及其索引的name属性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c643acf8-d44b-4d79-9666-7f1dc96319fe",
   "metadata": {},
   "source": [
    "Series对象本身及其索引都有一个name属性，可赋值设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "062a41fa-0d45-4878-8ca9-0a3899155a0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "p3    23\n",
       "p1    45\n",
       "p2    67\n",
       "p5    89\n",
       "dtype: int64"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "effde3d6-7101-40ea-a6cb-4c9db1c05a1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_num.name = 'ProductNums' # 本身名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "cdc79817-4283-44cf-93b1-017c121b3d9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_num.index.name = 'ProductType' # 索引名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "39a653ec-f941-4400-b9d3-ab640428626b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ProductType\n",
       "p3    23\n",
       "p1    45\n",
       "p2    67\n",
       "p5    89\n",
       "Name: ProductNums, dtype: int64"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e06151d1-ae42-4c05-88aa-236290996ef6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
